﻿

''' <summary>
''' 压缩机组数据访问类
''' </summary>
''' <remarks></remarks>
Public Class CcapCompressorSetItemService

    'Private _Dev As CcapDevItem = Nothing

    '''' <summary>
    '''' 构造函数
    '''' </summary>
    '''' <param name="dev">设备信息</param>
    '''' <remarks></remarks>
    'Public Sub New(ByVal dev As CcapDevItem)
    '_Dev = dev
    'End Sub

    ''' <summary>
    ''' 默认构造函数
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' 通过机组编号获取压缩机组信息
    ''' </summary>
    ''' <param name="csId">机组编号</param>
    ''' <returns>返回单个机组信息</returns>
    ''' <remarks></remarks>
    Public Function GetCompressorSet(ByVal csId As Integer) As DataSet
        Dim ds As DataSet = Nothing

        Dim strSql As String = String.Empty
        strSql = "select `Id`,`csName`,`csType`,`csCount`,`DevId` from tbccCompressorSet where `Id` =" + csId.ToString()
        Try
            ds = SqlHelper.ExecuteDataset(strSql, Nothing)
        Catch ex As Exception
            ds = Nothing
        End Try

        Return ds
    End Function

    ''' <summary>
    ''' 通过项目编号获取机组信息
    ''' </summary>
    ''' <param name="projectId">项目编号</param>
    ''' <returns>返回机组信息数据集</returns>
    ''' <remarks>数据集包含的字段有： Id, DevId, csName,csType, csCount, csNetId</remarks>
    Public Function GetCompressorSets(ByVal projectId As String) As DataSet
        Dim ds As DataSet = Nothing

        Dim strSql As String = String.Empty
        strSql = " SELECT CS.Id AS Id,CS.DevId AS DevId,CS.csName AS csName,CS.csType AS csType,CS.csCount AS csCount,Dev.csNetId AS csNetId " + _
                " FROM tbcccompressorset AS CS Inner Join tbccccapdevtype AS Dev ON CS.DevId = Dev.Id  " + _
                 "WHERE Dev.ProjectId =  '" + projectId + "'  AND Dev.csNetId >=  50"
        Try
            ds = SqlHelper.ExecuteDataset(strSql, Nothing)
        Catch ex As Exception
            ds = Nothing
        End Try

        Return ds
    End Function
    ''' <summary>
    ''' 返回所有机组信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GeRefGroupList(ByVal refTypeList As CcapDevItem) As List(Of CcapCompressorSetItem)
        Dim il As IList(Of CcapCompressorSetItem)
        il = New List(Of CcapCompressorSetItem)

        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("SELECT CS.Id AS Id,CS.DevId AS DevId,CS.csName AS csName,CS.csType AS csType,CS.csCount AS csCount  FROM tbcccompressorset AS CS Inner Join tbccccapdevtype AS Dev  on CS.DevId = Dev.id WHERE Dev.ProjectId ='{0}' and CS.DevId<>'{1}' order by id", MainForm.pi.ProjectID, refTypeList.Id)

        ds = TBCC.Utility.SqlHelper.ExecuteDataset(strSql, Nothing)
        If ds.Tables.Count = 0 OrElse ds.Tables(0).Rows.Count = 0 Then
            ds.Clear() : ds.Dispose() : ds = Nothing
            Return Nothing
        End If

        For Each r As DataRow In ds.Tables(0).Rows
            Dim rti As New CcapCompressorSetItem
            rti.Id = r("id")
            rti.devtId = r("Devid")
            ' rti.NetId =
            rti.CompressorSetName = r("csname")
            rti.CompressorCount = r("csCount")
            rti.CompressorSetType = r("cstype")

            il.Add(rti)

        Next

        Return il

    End Function
    ''' <summary>
    ''' 增加机组信息
    ''' </summary>
    ''' <param name="com"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function AddCcapDevItem(ByVal com As CcapCompressorSetItem) As Boolean
        Dim strSql As String

        Dim tic As New TBCC.Utility.TranstrationItem


        strSql = "Insert into tbccCompressorSet(id,Devid,csName,csType,csCount,RelId) Values(?p0,?p1,?p2,?p3,?p4,?p5)"

        Dim p1(5) As System.Data.Common.DbParameter

        p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(0).ParameterName = "p0" : p1(0).Value = com.Id

        p1(1) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(1).ParameterName = "p1" : p1(1).Value = com.devtId

        p1(2) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(2).ParameterName = "p2" : p1(2).Value = com.CompressorSetName

        p1(3) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(3).ParameterName = "p3" : p1(3).Value = com.CompressorSetType

        p1(4) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(4).ParameterName = "p4" : p1(4).Value = com.CompressorCount

        p1(5) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(5).ParameterName = "p5" : p1(5).Value = com.RelId

        tic.Add(strSql, p1)


        Return TBCC.Utility.SqlHelper.ExecuteTranstration(tic)
    End Function
    ''' <summary>
    ''' 删除机组信息
    ''' </summary>
    ''' <param name="com"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DelCcapDevItem(ByVal com As Integer) As Boolean
        Dim strSql As String

        Dim tic As New TBCC.Utility.TranstrationItem

        strSql = String.Format("delete from tbccCompressorSet where id='{0}'", com)

        tic.Add(strSql, Nothing)

        Return TBCC.Utility.SqlHelper.ExecuteTranstration(tic)
    End Function
    ''' <summary>
    ''' 更新机组信息
    ''' </summary>
    ''' <param name="com"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function UpdateCcapDevItem(ByVal com As CcapCompressorSetItem) As Boolean

        Dim strSql As String

        Dim tic As New TBCC.Utility.TranstrationItem

        strSql = String.Format("Update tbccCompressorSet set csCount='{0}',Devid='{1}',csName='{2}' ,csType='{3}'  where id='{4}'", com.CompressorCount, com.devtId, com.CompressorSetName, com.CompressorSetType, com.Id)

        tic.Add(strSql, Nothing)

        Return TBCC.Utility.SqlHelper.ExecuteTranstration(tic)
    End Function
    ''' <summary>
    ''' 根据机组ID获取机组类型
    ''' </summary>
    ''' <param name="csid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetCsTypeItem(ByVal csid As Integer) As Integer
        Dim strSql As String
        strSql = String.Format("Select csType from tbccCompressorSet where id='{0}'", csid)

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return 0
        Else
            Return CInt(obj)
        End If


    End Function
    ''' <summary>
    ''' 根据机组id返回机组配置实际ID
    ''' </summary>
    ''' <param name="csid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetCsRelIdItem(ByVal csid As Integer) As Integer
        Dim strSql As String
        strSql = String.Format("Select RelId from tbccCompressorSet where id='{0}'", csid)

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return 0
        Else
            Return CInt(obj)
        End If


    End Function
End Class
